<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use App\model\broker\BrokerProject;
use App\model\Workshop;
use App\model\Land;
use App\model\Building;
use Illuminate\Support\Facades\Auth;
use App\Repository\ShareRepository;
use App\model\broker\BrokerRewardRecord;

class DistributionController extends Controller
{
    //
    public function index(Request $request,$id,$user_id=null){
        $brokerProject = BrokerProject::find($id);
        $type = $brokerProject->type;
        $projectId = $brokerProject->produce_id;
        if($type == 0){
            $project = Workshop::find($projectId);
        }else if($type == 1){
            $project = Land::find($projectId);
        }else if($type == 2){
            $project = Building::find($projectId);
        }
        $user = Auth::user();
        if($user_id != null){
            $fromUser = new User();
            $fromUser->id = $user_id;

            $toUser = new User();
            $toUser->id = $user->id;

            $shareRepository = new ShareRepository();
            $shareRepository->bindUser($fromUser,$toUser,$brokerProject);
        }
        $rewards = BrokerRewardRecord::with('fromUser')->get();
        //dd($rewards);
        return view('distribution',['brokerProject'=>$brokerProject,'project'=>$project,'user'=>$user,'reward'=>$rewards]);
    }
}
